﻿using MCPlatform.Models;
using MCPlatform.MotionLib.Factory;
using NLog.Config;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MCPlatform.UI.Controls.Models;
using MCPlatform.MotionLib.Base.AssitantClass;
using MCPlatform.MotionLib.Base.Models;
using MCPlatform.MotionLib.Base.Environment;
using MCPlatform.ViewModels.Shell;

namespace MCPlatform.Builder.WorkStation
{
    public class WorkStation
    {


        #region 懒汉单例模式
        private static readonly Lazy<WorkStation> lazyInstance = new Lazy<WorkStation>(() => new WorkStation());

        private WorkStation()
        {
            ConstructStation();
        }

        public static WorkStation Instance
        {
            get
            {
                return lazyInstance.Value;
            }
        }
        #endregion

        public Dictionary<int, WorkStationModel> WorkStationDic { get; set; } = new Dictionary<int, WorkStationModel>();

        public Factory Factory = Factory.GetInstance(new LTFactory(), 8, 11, 29);

        public List<PointSettingModel>? PointSettingDic { get; set; }
        private void ConstructStation()
        {
            //Points
            PointSettingDic = ReadConfigData.GetParaConfigs<PointSettingModel>(Paths.PointConfigPaths);


            //构建工站
            ConstructLoadStation();

            ConstructCuttingStation();


            ConstructUnLoadStation();

            ConstructMainControlStation();



        }

        private void ConstructLoadStation()
        {
            //上料工站
            WorkStationModel LoadStation = new WorkStationModel()
            {
                WorkStationName = "上料模块",
                WorkStationKey = "LoadStation",
                WorkStationID = 1,
                AxisCount = 1,
                ValveCount = 2,
                IOCount = 1,
                PointsCount = 2,


            };
            //Axis
            LoadStation.AxisModels[0].MotionEntity = Factory.Motions?["Load Drag Axis"];

            //Valves
            LoadStation.CylinderModels[0].ValveEntity = Factory.Valves?["Load Jack Valve"];
            LoadStation.CylinderModels[1].ValveEntity = Factory.Valves?["Pull Drag Valve"];


            //IOs
            LoadStation.IOModels[0].IOEntity = Factory.IOs?["Leather Inplace Sensor"];


            LoadStation.PointSettingModels[0] = PointSettingDic?.FirstOrDefault(point => point.ControlName == "预先动作时间")
                  ?? new PointSettingModel()
                  {
                      ControlName = "预先动作时间",
                      PointDescription = "时间",
                      Parameter = 1000,
                      Unit = "ms",
                      IsPosition = false,
                      MotionEntity = Factory.Motions?["Load Drag Axis"]
                  };
            // LoadStation.PointSettingModels[0].MotionEntity = Factory.Motions["Load Z Axis"];

            LoadStation.PointSettingModels[1] = PointSettingDic?.FirstOrDefault(point => point.ControlName == "牵引速度比例")
                 ?? new PointSettingModel()
                 {
                     ControlName = "牵引速度比例",
                     PointDescription = "比例",
                     Parameter = 1.2,
                     IsPosition = false,
                     Unit = "倍",
                     MotionEntity = Factory.Motions?["Load Drag Axis"]
                 };





            WorkStationDic.Add(1, LoadStation);
        }


        private void ConstructCuttingStation()
        {
            //切割工站
            WorkStationModel CuttingStation = new WorkStationModel()
            {
                WorkStationName = "切割加工模块",
                WorkStationKey = "CuttingStation",
                WorkStationID = 2,
                AxisCount = 4,
                ValveCount = 9,
                IOCount = 7,
                PointsCount = 10,
                CoordinateCount = 1,

            };
            CuttingStation.AxisModels[0].MotionEntity = Factory.Motions?["Cutting X Axis"];
            CuttingStation.AxisModels[1].MotionEntity = Factory.Motions?["Cutting Y Axis"];
            CuttingStation.AxisModels[2].MotionEntity = Factory.Motions?["Cutting Z Axis"];
            CuttingStation.AxisModels[3].MotionEntity = Factory.Motions?["Cutting U Axis"];
            //Valves
            CuttingStation.CylinderModels[0].ValveEntity = Factory.Valves?["Cutting Press Vacuum"];
            CuttingStation.CylinderModels[1].ValveEntity = Factory.Valves?["Cutting Transport Valve"];
            CuttingStation.CylinderModels[2].ValveEntity = Factory.Valves?["Cutting Finger Valve"];
            CuttingStation.CylinderModels[3].ValveEntity = Factory.Valves?["Cutting Press Valve"];
            CuttingStation.CylinderModels[4].ValveEntity = Factory.Valves?["Cutting Punch Valve"];
            CuttingStation.CylinderModels[5].ValveEntity = Factory.Valves?["Cutting Collect Valve"];
            CuttingStation.CylinderModels[6].ValveEntity = Factory.Valves?["Cutting Panel Vacuum"];
            CuttingStation.CylinderModels[7].ValveEntity = Factory.Valves?["Cutting Punch Vacuum"];
            CuttingStation.CylinderModels[8].ValveEntity = Factory.Valves?["Cutting Panel Vacuum2"];


            CuttingStation.CoordinateModels[0].ImputationEntity = Factory.Imputations?["Cutting Coordinate"];
            //IOs
            CuttingStation.IOModels[0].IOEntity = Factory.IOs?["Infrared Safety Sensor1"];
            CuttingStation.IOModels[1].IOEntity = Factory.IOs?["Infrared Safety Sensor2"];
            CuttingStation.IOModels[2].IOEntity = Factory.IOs?["Punching Motor"];
            CuttingStation.IOModels[3].IOEntity = Factory.IOs?["Cutting Motor"];
            CuttingStation.IOModels[4].IOEntity = Factory.IOs?["Cross Positioning Light"];
            CuttingStation.IOModels[5].IOEntity = Factory.IOs?["Forward Motor"];

            // CuttingStation.IOModels[6].IOEntity = Factory.IOs["Backward Motor"];
            // CuttingStation.IOModels[7].IOEntity = Factory.IOs["Blower On"];
            // CuttingStation.IOModels[8].IOEntity = Factory.IOs["Blower In"];

            CuttingStation.IOModels[6].IOEntity = Factory.IOs?["Light"];


            CuttingStation.PointSettingModels[0] = PointSettingDic?.FirstOrDefault(point => point.ControlName == "切割X轴等待位置")
              ?? new PointSettingModel()
              {
                  ControlName = "切割X轴等待位置",
                  PointDescription = "位置",
                  Parameter = 0,
                  IsPosition = true,
                  MotionEntity = Factory.Motions?["Cutting X Axis"],
              };
            CuttingStation.PointSettingModels[0].MotionEntity = Factory.Motions?["Cutting X Axis"];

            CuttingStation.PointSettingModels[1] = PointSettingDic?.FirstOrDefault(point => point.ControlName == "切割Y轴等待位置")
             ?? new PointSettingModel()
             {
                 ControlName = "切割Y轴等待位置",
                 PointDescription = "位置",
                 Parameter = 0,
                 IsPosition = true,
                 MotionEntity = Factory.Motions?["Cutting Y Axis"],
             };
            CuttingStation.PointSettingModels[1].MotionEntity = Factory.Motions?["Cutting Y Axis"];


            CuttingStation.PointSettingModels[2] = PointSettingDic?.FirstOrDefault(point => point.ControlName == "切割Z轴等待位置")
             ?? new PointSettingModel()
             {
                 ControlName = "切割Z轴等待位置",
                 PointDescription = "位置",
                 Parameter = 0,
                 IsPosition = true,
                 MotionEntity = Factory.Motions?["Cutting Z Axis"],
             };
            CuttingStation.PointSettingModels[2].MotionEntity = Factory.Motions?["Cutting Z Axis"];

            CuttingStation.PointSettingModels[3] = PointSettingDic?.FirstOrDefault(point => point.ControlName == "切割U轴等待位置")
             ?? new PointSettingModel()
             {
                 ControlName = "切割U轴等待位置",
                 PointDescription = "位置",
                 Parameter = 0,
                 IsPosition = true,
                 MotionEntity = Factory.Motions?["Cutting U Axis"],
             };
            CuttingStation.PointSettingModels[3].MotionEntity = Factory.Motions?["Cutting U Axis"];


            //CuttingStation.PointSettingModels[4] = PointSettingDic?.FirstOrDefault(point => point.ControlName == "切割X轴放料位置")
            // ?? new PointSettingModel()
            // {
            //     ControlName = "切割X轴放料位置",
            //     PointDescription = "位置",
            //     Parameter = 0,
            //     IsPosition = true,
            //     MotionEntity = Factory.Motions?["Cutting X Axis"],
            // };
            //CuttingStation.PointSettingModels[4].MotionEntity = Factory.Motions?["Cutting X Axis"];


            //CuttingStation.PointSettingModels[5] = PointSettingDic?.FirstOrDefault(point => point.ControlName == "切割Y轴分料开始位置")
            //?? new PointSettingModel()
            //{
            //    ControlName = "切割Y轴分料开始位置",
            //    PointDescription = "位置",
            //    Parameter = 0,
            //    IsPosition = true,
            //    MotionEntity = Factory.Motions?["Cutting Y Axis"],
            //};
            //CuttingStation.PointSettingModels[5].MotionEntity = Factory.Motions?["Cutting Y Axis"];


            CuttingStation.PointSettingModels[4] = PointSettingDic?.FirstOrDefault(point => point.ControlName == "切割Z轴切料位置")
           ?? new PointSettingModel()
           {
               ControlName = "切割Z轴切料位置",
               PointDescription = "位置",
               Parameter = 0,
               IsPosition = true,
               MotionEntity = Factory.Motions?["Cutting Z Axis"],
           };
            CuttingStation.PointSettingModels[4].MotionEntity = Factory.Motions?["Cutting Z Axis"];


            //CuttingStation.PointSettingModels[7] = PointSettingDic?.FirstOrDefault(point => point.ControlName == "切割X轴回收废料位置")
            //?? new PointSettingModel()
            //{
            //    ControlName = "切割X轴回收废料位置",
            //    PointDescription = "位置",
            //    Parameter = 0,
            //    IsPosition = true,
            //    MotionEntity = Factory.Motions?["Cutting X Axis"],
            //};
            //CuttingStation.PointSettingModels[7].MotionEntity = Factory.Motions?["Cutting X Axis"];


            CuttingStation.PointSettingModels[5] = PointSettingDic?.FirstOrDefault(point => point.ControlName == "废料回收电机旋转时间")
           ?? new PointSettingModel()
           {
               ControlName = "废料回收电机旋转时间",
               PointDescription = "时间",
               Unit = "ms",
               Parameter = 5000,
               IsPosition = false,

           };

            //CuttingStation.PointSettingModels[9] = PointSettingDic?.FirstOrDefault(point => point.ControlName == "切割X轴开始切料位置")
            // ?? new PointSettingModel()
            // {
            //     ControlName = "切割X轴开始切料位置",
            //     PointDescription = "位置",
            //     Parameter = 0,
            //     IsPosition = true,
            //     MotionEntity = Factory.Motions?["Cutting X Axis"],
            // };
            //CuttingStation.PointSettingModels[9].MotionEntity = Factory.Motions?["Cutting X Axis"];


            //CuttingStation.PointSettingModels[10] = PointSettingDic?.FirstOrDefault(point => point.ControlName == "切割Y轴开始切料位置")
            //?? new PointSettingModel()
            //{
            //    ControlName = "切割Y轴开始切料位置",
            //    PointDescription = "位置",
            //    Parameter = 0,
            //    IsPosition = true,
            //    MotionEntity = Factory.Motions?["Cutting Y Axis"],
            //};
            //CuttingStation.PointSettingModels[10].MotionEntity = Factory.Motions?["Cutting Y Axis"];


            //CuttingStation.PointSettingModels[11] = PointSettingDic?.FirstOrDefault(point => point.ControlName == "切割X轴拍照位置")
            //?? new PointSettingModel()
            //{
            //    ControlName = "切割X轴拍照位置",
            //    PointDescription = "位置",
            //    Parameter = 0,
            //    IsPosition = true,
            //    MotionEntity = Factory.Motions?["Cutting X Axis"],
            //};
            //CuttingStation.PointSettingModels[11].MotionEntity = Factory.Motions?["Cutting X Axis"];


            //CuttingStation.PointSettingModels[12] = PointSettingDic?.FirstOrDefault(point => point.ControlName == "切割Y轴拍照位置")
            //?? new PointSettingModel()
            //{
            //    ControlName = "切割Y轴拍照位置",
            //    PointDescription = "位置",
            //    Parameter = 0,
            //    IsPosition = true,
            //    MotionEntity = Factory.Motions?["Cutting Y Axis"],
            //};
            //CuttingStation.PointSettingModels[12].MotionEntity = Factory.Motions?["Cutting Y Axis"];


            //CuttingStation.PointSettingModels[13] = PointSettingDic?.FirstOrDefault(point => point.ControlName == "切割X轴上料位置")
            //  ?? new PointSettingModel()
            //  {
            //      ControlName = "切割X轴上料位置",
            //      PointDescription = "位置",
            //      Parameter = 0,
            //      IsPosition = true,
            //      MotionEntity = Factory.Motions?["Cutting X Axis"],
            //  };
            //CuttingStation.PointSettingModels[13].MotionEntity = Factory.Motions?["Cutting X Axis"];

            //CuttingStation.PointSettingModels[14] = PointSettingDic?.FirstOrDefault(point => point.ControlName == "切割X轴分料位置")
            // ?? new PointSettingModel()
            // {
            //     ControlName = "切割X轴分料位置",
            //     PointDescription = "位置",
            //     Parameter = 0,
            //     IsPosition = true,
            //     MotionEntity = Factory.Motions?["Cutting X Axis"],
            // };
            //CuttingStation.PointSettingModels[14].MotionEntity = Factory.Motions?["Cutting X Axis"];


            //CuttingStation.PointSettingModels[15] = PointSettingDic?.FirstOrDefault(point => point.ControlName == "切割Y轴分料终点位置")
            //?? new PointSettingModel()
            //{
            //    ControlName = "切割Y轴分料终点位置",
            //    PointDescription = "位置",
            //    Parameter = 0,
            //    IsPosition = true,
            //    MotionEntity = Factory.Motions?["Cutting Y Axis"],
            //};
            //CuttingStation.PointSettingModels[15].MotionEntity = Factory.Motions?["Cutting Y Axis"];

            //CuttingStation.PointSettingModels[16] = PointSettingDic?.FirstOrDefault(point => point.ControlName == "切割X轴吸起废料位置")
            //?? new PointSettingModel()
            //{
            //    ControlName = "切割X轴吸起废料位置",
            //    PointDescription = "位置",
            //    Parameter = 0,
            //    IsPosition = true,
            //    MotionEntity = Factory.Motions["Cutting X Axis"],
            //};
            //CuttingStation.PointSettingModels[16].MotionEntity = Factory.Motions["Cutting X Axis"];



            CuttingStation.PointSettingModels[6] = PointSettingDic?.FirstOrDefault(point => point.ControlName == "下压吸嘴吸真空时间")
             ?? new PointSettingModel()
             {
                 ControlName = "下压吸嘴吸真空时间",
                 PointDescription = "时间",
                 Parameter = 1000,
                 IsPosition = false,
                 Unit = "[ms]",
             };


            CuttingStation.PointSettingModels[7] = PointSettingDic?.FirstOrDefault(point => point.ControlName == "切割X轴松开夹爪后后移距离")
            ?? new PointSettingModel()
            {
                ControlName = "切割X轴松开夹爪后后移距离",
                PointDescription = "距离",
                Parameter = 100,
                IsPosition = false,
                Unit = "[mm]",
            };

            CuttingStation.PointSettingModels[8] = PointSettingDic?.FirstOrDefault(point => point.ControlName == "振动刀下降到位延迟时间")
              ?? new PointSettingModel()
              {
                  ControlName = "振动刀下降到位延迟时间",
                  PointDescription = "时间",
                  Parameter = 1000,
                  IsPosition = false,
                  Unit = "[ms]",
              };

            CuttingStation.PointSettingModels[9] = PointSettingDic?.FirstOrDefault(point => point.ControlName == "切割X轴拉料速度")
             ?? new PointSettingModel()
             {
                 ControlName = "切割X轴拉料速度",
                 PointDescription = "速度",
                 Parameter = 100,
                 IsPosition = false,
                 Unit = "[mm/s]",
             };

            WorkStationDic.Add(2, CuttingStation);
        }


        private void ConstructUnLoadStation()
        {
            //下料工站
            WorkStationModel UnloadStation = new WorkStationModel()
            {
                WorkStationName = "下料码垛模块",
                WorkStationKey = "UnloadStation",
                WorkStationID = 3,
                AxisCount = 3,
                ValveCount = 1,
                IOCount = 6,
                PointsCount = 5,


            };
            UnloadStation.AxisModels[0].MotionEntity = Factory.Motions?["Unload X Axis"];
            UnloadStation.AxisModels[1].MotionEntity = Factory.Motions?["Unload Y Axis"];
            UnloadStation.AxisModels[2].MotionEntity = Factory.Motions?["Unload Z Axis"];

            //Valves
            UnloadStation.CylinderModels[0].ValveEntity = Factory.Valves?["Unload Vacuum"];

            //IOS
            UnloadStation.IOModels[0].IOEntity = Factory.IOs?["Infrared Safety Sensor3"];
            UnloadStation.IOModels[1].IOEntity = Factory.IOs?["Infrared Safety Sensor4"];
            UnloadStation.IOModels[2].IOEntity = Factory.IOs?["Infrared Safety Sensor5"];
            UnloadStation.IOModels[3].IOEntity = Factory.IOs?["Infrared Safety Sensor6"];
            UnloadStation.IOModels[4].IOEntity = Factory.IOs?["Infrared Safety Sensor7"];
            UnloadStation.IOModels[5].IOEntity = Factory.IOs?["Ionizer"];


            UnloadStation.PointSettingModels[0] = PointSettingDic?.FirstOrDefault(point => point.ControlName == "下料X等待位置")
                 ?? new PointSettingModel()
                 {
                     ControlName = "下料X等待位置",
                     PointDescription = "位置",
                     Parameter = 0,
                     IsPosition = true,
                 };
            UnloadStation.PointSettingModels[0].MotionEntity = Factory.Motions?["Unload X Axis"];

            UnloadStation.PointSettingModels[1] = PointSettingDic?.FirstOrDefault(point => point.ControlName == "下料Y等待位置")
                ?? new PointSettingModel()
                {
                    ControlName = "下料Y等待位置",
                    PointDescription = "位置",
                    Parameter = 0,
                    IsPosition = true,
                };
            UnloadStation.PointSettingModels[1].MotionEntity = Factory.Motions?["Unload Y Axis"];

            UnloadStation.PointSettingModels[2] = PointSettingDic?.FirstOrDefault(point => point.ControlName == "下料Z等待位置")
                ?? new PointSettingModel()
                {
                    ControlName = "下料Z等待位置",
                    PointDescription = "位置",
                    Parameter = 0,
                    IsPosition = true,
                };
            UnloadStation.PointSettingModels[2].MotionEntity = Factory.Motions?["Unload Z Axis"];


            //UnloadStation.PointSettingModels[3] = PointSettingDic?.FirstOrDefault(point => point.ControlName == "下料X取料位置")
            //    ?? new PointSettingModel()
            //    {
            //        ControlName = "下料X取料位置",
            //        PointDescription = "位置",
            //        Parameter = 0,
            //        IsPosition = true,
            //    };
            //UnloadStation.PointSettingModels[3].MotionEntity = Factory.Motions?["Unload X Axis"];

            //UnloadStation.PointSettingModels[4] = PointSettingDic?.FirstOrDefault(point => point.ControlName == "下料Y取料位置")
            //    ?? new PointSettingModel()
            //    {
            //        ControlName = "下料Y取料位置",
            //        PointDescription = "位置",
            //        Parameter = 0,
            //        IsPosition = true,
            //    };
            //UnloadStation.PointSettingModels[4].MotionEntity = Factory.Motions?["Unload Y Axis"];

            //UnloadStation.PointSettingModels[5] = PointSettingDic?.FirstOrDefault(point => point.ControlName == "下料Z取料位置")
            //    ?? new PointSettingModel()
            //    {
            //        ControlName = "下料Z取料位置",
            //        PointDescription = "位置",
            //        Parameter = 0,
            //        IsPosition = true,
            //    };
            //UnloadStation.PointSettingModels[5].MotionEntity = Factory.Motions?["Unload Z Axis"];

            UnloadStation.PointSettingModels[3] = PointSettingDic?.FirstOrDefault(point => point.ControlName == "下料Z码垛位置")
               ?? new PointSettingModel()
               {
                   ControlName = "下料Z码垛位置",
                   PointDescription = "位置",
                   Parameter = 0,
                   IsPosition = true,
               };
            UnloadStation.PointSettingModels[3].MotionEntity = Factory.Motions?["Unload Z Axis"];


            UnloadStation.PointSettingModels[4] = PointSettingDic?.FirstOrDefault(point => point.ControlName == "码垛吸嘴吸真空时间")
              ?? new PointSettingModel()
              {
                  ControlName = "码垛吸嘴吸真空时间",
                  PointDescription = "时间",
                  Parameter = 1000,
                  IsPosition = false,
                  Unit="[ms]",
              };


            WorkStationDic.Add(3, UnloadStation);
        }


        private void ConstructMainControlStation()
        {
            //下料工站
            WorkStationModel MainControlStation = new WorkStationModel()
            {
                WorkStationName = "主控模块",
                WorkStationKey = "MainControlStation",
                WorkStationID = 0,
                AxisCount = 0,
                ValveCount = 0,
                IOCount = 16,
                PointsCount = 0,


            };


            //IOS
            MainControlStation.IOModels[0].IOEntity = Factory.IOs?["Start"];
            MainControlStation.IOModels[1].IOEntity = Factory.IOs?["Stop"];
            MainControlStation.IOModels[2].IOEntity = Factory.IOs?["Reset"];
            MainControlStation.IOModels[3].IOEntity = Factory.IOs?["Manual"];
            MainControlStation.IOModels[4].IOEntity = Factory.IOs?["E_stop"];


            MainControlStation.IOModels[5].IOEntity = Factory.IOs?["Front Safety Grating"];
            MainControlStation.IOModels[6].IOEntity = Factory.IOs?["Rear Safety Grating"];

            MainControlStation.IOModels[7].IOEntity = Factory.IOs?["Start Led"];
            MainControlStation.IOModels[8].IOEntity = Factory.IOs?["Stop Led"];
            MainControlStation.IOModels[9].IOEntity = Factory.IOs?["Reset Led"];

            MainControlStation.IOModels[10].IOEntity = Factory.IOs?["E_stop Reset"];
            MainControlStation.IOModels[11].IOEntity = Factory.IOs?["Red Led"];

            MainControlStation.IOModels[12].IOEntity = Factory.IOs?["Green Led"];
            MainControlStation.IOModels[13].IOEntity = Factory.IOs?["Yellow Led"];
            MainControlStation.IOModels[14].IOEntity = Factory.IOs?["Buzzer"];

            MainControlStation.IOModels[15].IOEntity = Factory.IOs?["Safety Opto"];

            //  MainControlStation.IOModels[15].IOEntity = Factory.IOs["Light"];






            WorkStationDic.Add(0, MainControlStation);
        }







    }
}
